﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using C4F.DevKit.WebServices;
using C4F.DevKit.WebServices.Datasets;

namespace C4F.DevKit.WebServicesDemo
{
    public partial class AmazonControl : UserControl
    {   
        public AmazonControl()
        {
            InitializeComponent();
        }

        private void AmazonGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewSelectedCellCollection Cells = AmazonGrid.SelectedCells;
            if (Cells[0] is System.Windows.Forms.DataGridViewLinkCell)
            {
                DataGridViewLinkCell LinkCell = (DataGridViewLinkCell)Cells[0];
                DemoBrowser BrowserForm = new DemoBrowser((string)LinkCell.Value);
                BrowserForm.Show();
            }
        }

        private void AmazonControl_Load(object sender, EventArgs e)
        {
            Array CategoryArray = Enum.GetValues(typeof(SearchCategory));
            CategoryCombo.DataSource = CategoryArray;
            CategoryCombo.SelectedIndex = 5;
        }

        private void GoButton_Click(object sender, EventArgs e)
        {
            try
            {
                SearchCategory c = (SearchCategory)Enum.Parse(typeof(SearchCategory), CategoryCombo.Text, true);
                AmazonService.ApplicationKey = ApplicationKey.Text;
                AmazonResults = AmazonService.SearchItems_DS(c, SearchText.Text);
                AmazonGrid.DataSource = AmazonResults;
            }
            catch (System.Net.WebException ex)
            {
                StreamReader streamReader = new StreamReader(ex.Response.GetResponseStream());
                string responseData = streamReader.ReadToEnd();
                streamReader.Close();
                MessageBox.Show(responseData);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
 